<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Search extends MY_Controller {

     function __construct() {
        
		parent::__construct();
        $this->load->helper('url');
        $this->load->library('parser');
        
        $this->load->model('product_model', 'product', true);
        $this->load->model('pcategory_model', 'category', true);

        $this->buildNavigation();
        $this->buildSearchByConditionBox();
        $this->buildSupportBox();

		//$this->buildSlider();
		$this->buildCartBox();
		$this->buildCompareBox();
		$this->buildAdvBox();
     }

	public function _remap($method)
	{
		if ($method == 'search-by-keyword')
		{
			$this->searchByKeyword();
		}
		else if ($method == 'sale-off')
		{
			$keyword = $this->uri->segment(3, '');
			$offset = $this->uri->segment(5, 0);
			$this->searchSaleOff($keyword, $offset);
		}
		else if ($method == 'price')
		{
			$keyword = $this->uri->segment(3, '');
			$offset = $this->uri->segment(5, 0);
			$this->searchPrice($keyword, $offset);
		}
		else 
			$this->index();
	}
	
    function index($code = '') {
        $this->view($code);
    }

	function searchSaleOff($keyword, $offset = 0) {
        $this->searchMinMax('sale_off', $keyword, $offset);
    }
    
	function searchPrice($keyword, $offset = 0) {
        $this->searchMinMax('price', $keyword, $offset);
    }
    
	function searchMinMax($searchBy, $keyword, $offset = 0) {
    	$this->load->library('pagination');
    	
    	$minMax = explode('-', $keyword);
    	if (count($minMax) != 2){
    		$this->index();
    		return;
    	}
    	
    	$min = $minMax[0];
    	$max = $minMax[1];
    	
    	$cat_code = '_';
        $cat = -1;
        $catIds = -1;
        $message = '';
        
        $_min = $min;
        $_max = $max;
        if ($searchBy == 'sale_off'){
        	if ($_min == 'less'){
//        		$_min = (doubleval( $_min ) * 100) . '%'; 
        		$_max = (doubleval( $_max ) * 100) . '%'; 	
        	}
        	else if ($_max == 'greater'){
        		$_min = (doubleval( $_min ) * 100) . '%'; 
//        		$_max = (doubleval( $_max ) * 100) . '%'; 
        	}
        	else{
        		$_min = (doubleval( $_min ) * 100) . '%'; 
        		$_max = (doubleval( $_max ) * 100) . '%'; 
        	}
        }
        
        	
//        if ($searchBy == 'price')
//        	$message = lang();
        
        if(!empty($cat_code) && $cat_code != '_'){
        	$cat = $this->category->getByCode($cat_code);
    	
       		$catIds = getElementsOfNode($cat['id_pcategory']);
    		$catIds[] = $cat['id_pcategory'];
        }
            
    	$offset = (int) $this->uri->segment(5, 0);
        //$base_url = $this->base_url . 'admin/product/view/' . $category . '/';
        $base_url = site_url ( 'search/'. str_replace('-', '_', $searchBy) . '/' . $keyword . '/' . $cat_code);
        $total_rows = $this->product->countSearchMinMax($searchBy, $min, $max, $catIds); //countSearchMinMax($key, $catIds);
        $per_page = 18; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 5;
        
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
    	$data['site_url'] = site_url();
		
       	$data['base_url'] = base_url();
       
    	
    	$data['products'] = $this->product->searchMinMax($offset, $per_page, $searchBy, $min, $max, $catIds); //search($offset, $per_page, $key, $catIds);
    	
    	//print_r($data['products']);
   
        $lang = getLangCode();
        
        $data['lang'] =  $lang;
        
        if ($min == 'less'){
        	$message = lang('find_number_product_with_'. str_replace('-', '_', $searchBy) .'_less');
        	$message = sprintf($message, $total_rows, $_max ); 
        }
        else if ($max == 'greater'){
        	$message = lang('find_number_product_with_'. str_replace('-', '_', $searchBy) .'_greater');
        	$message = sprintf($message, $total_rows, $_min );
        }
        else{
        	$message = lang('find_number_product_with_'. str_replace('-', '_', $searchBy) .'_from_to');
        	$message = sprintf($message, $total_rows, $_min, $_max );
        }
        
        $data['find_number_product_with_keyword'] = $message;
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/search/grid_product_view', $data);
        $this->template->render();  
        
    }
    
function searchByKeyword($keyword = "", $cat_code = "_", $offset = 0) {
    	$this->load->library('pagination');
    	
    	$key = $this->input->post('keyword');
    	
    	if (empty($key)){
    		 $key = $this->uri->segment(3) ; //$
    	}
    	//echo "My key: " .  $keyword . $cat_code;
    	
    	//print_r($this->input->get());
    	
        $cat = -1;
        $catIds = -1;
        if(!empty($cat_code) && $cat_code != '_'){
        	$cat = $this->category->getByCode($cat_code);
    	
       		$catIds = getElementsOfNode($cat['id_pcategory']);
    		$catIds[] = $cat['id_pcategory'];
        }
            
    	$offset = (int) $this->uri->segment(5, 0);
        //$base_url = $this->base_url . 'admin/product/view/' . $category . '/';
        $base_url = site_url ( 'search/search-by-keyword/' . $key . '/' . $cat_code);
        $total_rows = $this->product->countSearch($key, $catIds);
        $per_page = 18; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 5;
        
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
    	$data['site_url'] = site_url();
		
       	$data['base_url'] = base_url();
       
    	if(!empty($key)){
			
    		$data['products'] = $this->product->search($offset, $per_page, $key, $catIds);
    	}
    	else {
    		$data['products'] = $this->product->getNewProduct();
    	}
    	
    	//print_r($data['products']);
   
        $lang = getLangCode();
        
        $data['lang'] =  $lang;
        $data['find_number_product_with_keyword'] = sprintf(lang('find_number_product_with_keyword'), $total_rows, $key);
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/search/grid_product_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
        
    }
    
    
    function view($code = "", $offset = 0, $number_record = 30) {
		
    	$this->load->library('pagination');
    	
        // echo '<br>' . $code_category . '<br>';
        //$category = $this->category->getByCode($code);
        $cat = -1;
        $catIds = -1;
        if(!empty($code)){
        	$cat = $this->category->getByCode($code);
    	
       		$catIds = getElementsOfNode($cat['id_pcategory']);
    		$catIds[] = $cat['id_pcategory'];
        }
            
    	$offset = (int) $offset;
        //$base_url = $this->base_url . 'admin/product/view/' . $category . '/';
        $base_url = site_url ( 'product/view/' . $code . '/');
        $total_rows = $this->product->countAll($catIds);
        $per_page = 24; //NUMBER_RECORD_PER_PRODUCT_PAGE;
        $uri_segment = 4;

		
		
        pagination_front($base_url, $total_rows, $per_page, $uri_segment);
        
    	$data['site_url'] = site_url();
		
       	$data['base_url'] = base_url();
       
    	if(!empty($code)){
			
    		$data['products'] = $this->product->getAll($offset, $per_page, $catIds);
    	}
    	else {
    		$data['products'] = $this->product->getNewProduct();
    	}
   
        $lang = getLangCode();
        
       
        $data['lang'] =  $lang;
        
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/product/grid_product_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
        
    }
    
	function detail($code) {
		
    	$data = $this->product->getByCode($code);
    	
    	$lang = getLangCode();
    	
    	$data['name'] = $data[$lang . '_name'];
    	$data['description'] = $data[$lang . '_description'];
    	
    	
    	$image = $data['image'];
		$upload_dir = getDirByDate($data['date_add'], PRODUCT_IMAGE_PATH);
		
		if(!file_exists($upload_dir.'/'.$image)) 
			$image = '';
		else 
			$image = img(array('src'=>base_url($upload_dir.'/' .$image), 'width' => '220px', 'height'=>'199px' ));
		
		$data['image'] = $image;
		$data['add_to_wishlist'] = lang('add_to_wishlist'); 
		$data['add_to_compare'] = lang('add_to_compare');
		$data['email_to_a_friend'] = lang('email_to_a_friend');
		$data['product_information'] = lang('product_information');
		
        $lang = getLangCode();
        
       
        $data['lang'] =  $lang;
        
        
        //$this->template->parse_view('center_content1', 'default/front_end/product/list_product_view', $data);
        $this->template->parse_view('center_content1', 'default/front_end/product/detail_view', $data);
        //$this->buildLeftContent();

        $this->template->render();  
        
	}

}

?>